from git.data import git_account_service
from git.data import git_account
from db import user

class GitAccountServiceImpl(git_account_service.GitAccountService):
    def GetAccountByEmail(self, email):
        account = None
        profile = user.GetUserByEmail(email)
        if profile:
            if profile.account_type == 1:
                account_type = git_account.GitAccount.FEDERATED
            else:
                account_type = git_account.GitAccount.LEGACY
            account = git_account.GitAccount(email, account_type)
            account.SetLocalId(email)
            account.SetDisplayName(profile.first_name + ' ' + profile.last_name)
            return account
        else:
            return None

    def CheckPassword(self, email, password):
        profile = user.GetUserByEmail(email)
        if profile:
            if profile.account_type == git_account.GitAccount.FEDERATED:
                return True
            else:
                return profile.password == password
        return False
    
    def ToFederated(self, email):
        profile = user.GetUserByEmail(email)
        profile.account_type = 1
        profile.password = ''
        profile.put()
        pass